package com.gba.mark.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gba.mark.group.entity.Group;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.user.auth.utils.AuthUtils;
import com.gba.mark.user.entity.user.User;
import com.gba.mark.user.service.user.IUserService;
import com.gba.mark.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private IUserService userServiceImpl;

    @PostMapping("/saveGroup")
    public ResponseUtil<Boolean> addGroup(Authentication authentication, @RequestParam String name,
                                       @RequestParam String language,
                                       @RequestParam int size) {
        String username = AuthUtils.getUsername(authentication);
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", username));
        int adminId = user.getId();
        System.out.println(adminId);
        return new ResponseUtil<>(HttpStatus.OK, "", groupService.saveGroup(language, name, size, adminId));
    }

    @DeleteMapping ("/removeGroup")
    public ResponseUtil<Boolean> removeGroup(Authentication authentication, @RequestParam String groupName) {
        String username = AuthUtils.getUsername(authentication);
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", username));
        int adminId = user.getId();
        return new ResponseUtil<>(HttpStatus.OK, "", groupService.removeGroup(groupName, adminId));
    }

    @GetMapping("/listGroup")
    public ResponseUtil<List<Group>> listGroup(Authentication authentication) {
        String username = AuthUtils.getUsername(authentication);
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", username));
        int adminId = user.getId();
        return new ResponseUtil<>(HttpStatus.OK, "", groupService.searchGroup(adminId));
    }
}
